﻿using System;
using System.Linq;
using Colligate;

namespace OrgDataSource
{
// ReSharper disable InconsistentNaming
    public class BranchDS:IOrgDS
// ReSharper restore InconsistentNaming
    {
        #region\\delete
        public int DeleteTaskData(OUDataInfo userInfo, string dataGuid, string dataType, orgEntities context)
        {
            switch (dataType.ToLower())
            {
                case "branch": return DeleteBranch(dataGuid, context);
                default: throw new Exception("BranchDS无法处理该" + dataType + "的数据");
            }
        }

        private static int DeleteBranch(string dataGuid, orgEntities context)
        {
            try
            {
                var branch = context.Branch.FirstOrDefault(cc => cc.Guid == dataGuid);
                context.DeleteObject(branch);
                var result = context.SaveChanges();
                return result == 1 ? 0 : 1;
            }
            catch
            {
                throw new Exception("删除数据标识为：" + dataGuid + "的Branch数据时出错！");
            }

        }
        #endregion
        #region\\get
        public System.Xml.XmlNode GetTaskData(OUDataInfo userInfo, string dataGuid, string dataType, orgEntities context, BaseTaskDataSource baseInfo)
        {
            switch (dataType.ToLower())
            {
                case "branch": return GetBranch(dataGuid, context);
                case "branchlist": return GetBranchList(dataGuid, context);
                case "branchmemberlist": return GetBranchMemberList(dataGuid, context);
                case "branchmemberdetail": return GetBranchMemberDetail(dataGuid, context);
                default: throw new Exception("BranchDS无法处理该" + dataType + "的数据");
            }
        }

        private static System.Xml.XmlNode GetBranch(string dataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "Branch");
                var branch = context.Branch.FirstOrDefault(cc => cc.Guid == dataGuid) ?? new Branch
                                                                                             {
                                                                                                 Guid = string.Empty,
                                                                                                 Name = string.Empty,
                                                                                                 CompanyGuid = string.Empty,
                                                                                                 ParentGuid=string.Empty,
                                                                                                 MasterGuid = string.Empty,
                                                                                                 LeaderGuid = string.Empty,
                                                                                                 Telephone = string.Empty,
                                                                                                 EMail = string.Empty,
                                                                                                 Sort = null
                                                                                             };
                XmlNodeManage.SetSubValue(result, "Guid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.Guid);
                XmlNodeManage.SetSubValue(result, "Name", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.Name);
                XmlNodeManage.SetSubValue(result, "CompanyGuid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.CompanyGuid);
                XmlNodeManage.SetSubValue(result, "ParentGuid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.ParentGuid);
                XmlNodeManage.SetSubValue(result, "MasterGuid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.MasterGuid);
                XmlNodeManage.SetSubValue(result, "LeaderGuid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.LeaderGuid);
                XmlNodeManage.SetSubValue(result, "Telephone", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.Telephone);
                XmlNodeManage.SetSubValue(result, "EMail", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), branch.EMail);
                XmlNodeManage.SetSubValue(result, "Order", Enum.GetName(typeof(TypeEnum), TypeEnum.LONG), branch.Sort);
                return result;
            }
            catch
            {
                throw new Exception("获取数据标识为：" + dataGuid + "的Branch数据时出错！");
            }
        }
        private static System.Xml.XmlNode GetBranchList(string dataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "BranchList");
                var subNode = XmlNodeManage.CreateNode("ListOUDataInfo");
                var company = context.Company.FirstOrDefault(cc => cc.Guid == dataGuid);
                System.Collections.Generic.List<Branch> branch;
                if (null == company)
                    branch =
                        context.Branch.Where(cc => cc.ParentGuid == dataGuid).OrderByDescending(cc => cc.Sort).ToList();
                else
                    branch =
                        context.Branch.Where(cc => cc.ParentGuid == "" && cc.CompanyGuid == dataGuid).OrderByDescending(
                            cc => cc.Sort).ToList();
                foreach (var item in branch)
                {
                    var childNode = XmlNodeManage.CreateNode("OUDataInfo");
                    XmlNodeManage.SetSubValue(childNode, "@GUID", item.Guid);
                    XmlNodeManage.SetSubValue(childNode, "@NAME", item.Name);
                    XmlNodeManage.SetSubValue(childNode, "@OUType", "Branch");
                    XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", item.CompanyGuid);
                    //if (flag)
                    //    childNode.InnerText = GetBranchPath(item, branch, string.Empty);
                    subNode.AppendChild(childNode);
                }
                result.AppendChild(subNode);
                return result;
            }
            catch
            {
                throw new Exception("获取的BranchList数据时出错！");
            }
        }
        private static System.Xml.XmlNode GetBranchMemberList(string dataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "BranchMemberList");
                var subNode = XmlNodeManage.CreateNode("ListOUDataInfo");
                var member = context.Member.Where(cc => cc.BranchGuid == dataGuid).OrderByDescending(cc => cc.Sort);
                var companyGuid = Utility.GetCompanyGuid(dataGuid, context.Branch.ToArray());
                foreach (var item in member)
                {
                    var childNode = XmlNodeManage.CreateNode("OUDataInfo");
                    XmlNodeManage.SetSubValue(childNode, "@GUID", item.Guid);
                    XmlNodeManage.SetSubValue(childNode, "@NAME", item.Name);
                    XmlNodeManage.SetSubValue(childNode, "@OUType", "Member");
                    XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", companyGuid);
                    subNode.AppendChild(childNode);
                }
                result.AppendChild(subNode);
                return result;
            }
            catch
            {
                throw new Exception("获取的BranchMemberList数据时出错！");
            }
        }
        private static System.Xml.XmlNode GetBranchMemberDetail(string dataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "BranchMemberDetail");
                var branch = context.Branch.FirstOrDefault(cc => cc.Guid == dataGuid);
                var member = context.Member.Where(cc => cc.BranchGuid == dataGuid).OrderByDescending(cc => cc.Sort).ToList();
                //var account=context.Account.FirstOrDefault(cc=>cc.Guid==member.)
                //var companyGuid = Utility.GetCompanyGuid(dataGuid, context.Branch.ToArray());
                foreach (var item in member)
                {
                    var subNode = XmlNodeManage.CreateNode("Member");
                    var accuotn = context.Account.FirstOrDefault(cc => cc.Guid == item.Guid);
                    XmlNodeManage.SetSubValue(subNode, "GUID", Enum.GetName(typeof (TypeEnum), TypeEnum.GUID), item.Guid);
                    XmlNodeManage.SetSubValue(subNode, "NAME", Enum.GetName(typeof (TypeEnum), TypeEnum.TEXT), item.Name);
                    XmlNodeManage.SetSubValue(subNode, "Account", Enum.GetName(typeof (TypeEnum), TypeEnum.TEXT),
                                              accuotn.AccountName);
                    XmlNodeManage.SetSubValue(subNode, "AccountStatus", Enum.GetName(typeof (TypeEnum), TypeEnum.LOGICAL),
                                              accuotn.Status);
                    XmlNodeManage.SetSubValue(subNode, "Account", Enum.GetName(typeof (TypeEnum), TypeEnum.TEXT),
                                              accuotn.AccountName);
                    XmlNodeManage.SetSubValue(subNode, "IsEvection", Enum.GetName(typeof (TypeEnum), TypeEnum.LOGICAL),
                                              accuotn.IsEvection);
                    XmlNodeManage.SetSubValue(subNode, "Telephone", Enum.GetName(typeof (TypeEnum), TypeEnum.TEXT),
                                              item.Telephone);
                    XmlNodeManage.SetSubValue(subNode, "Handphone1", Enum.GetName(typeof (TypeEnum), TypeEnum.TEXT),
                                              item.Handphone1);
                    XmlNodeManage.SetSubValue(subNode, "Handphone2", Enum.GetName(typeof (TypeEnum), TypeEnum.TEXT),
                                              item.Handphone2);
                    XmlNodeManage.SetSubValue(subNode, "EMail", Enum.GetName(typeof (TypeEnum), TypeEnum.TEXT),
                                              item.EMail);
                    XmlNodeManage.SetSubValue(subNode, "Order", Enum.GetName(typeof (TypeEnum), TypeEnum.LONG),
                                              item.Sort);
                    XmlNodeManage.SetSubValue(subNode, "IsLeader", Enum.GetName(typeof (TypeEnum), TypeEnum.LOGICAL),
                                              null==branch.LeaderGuid||branch.LeaderGuid == dataGuid ? "true" : "false");
                    XmlNodeManage.SetSubValue(subNode, "IsMaster", Enum.GetName(typeof (TypeEnum), TypeEnum.LOGICAL),
                                              null==branch.MasterGuid||branch.MasterGuid == dataGuid ? "true" : "false");
                    result.AppendChild(subNode);
                }
                return result;
            }
            catch
            {
                throw new Exception("获取的BranchMemberList数据时出错！");
            }
        }
        #endregion
        #region\\set
        public int SetTaskData(OUDataInfo userInfo, string dataGuid, string dataType, System.Xml.XmlNode xmlData, orgEntities context)
        {
            var datatype = XmlNodeManage.GetSubValue(xmlData, "@DataType");
            switch (datatype.ToLower())
            {
                case "branch": return SetBranch(xmlData, context);
                default: throw new Exception("BranchDS无法处理该" + datatype + "的数据");
            }
        }

        private static int SetBranch(System.Xml.XmlNode xmlData, orgEntities context)
        {
            try
            {
                var id = Utility.CheckXmlData(xmlData,"Guid");
                var branch = context.Branch.FirstOrDefault(cc => cc.Guid == id);
                if (null == branch)
                {
                    branch = new Branch
                                 {
                                     Guid = id,
                                     Name = Utility.CheckXmlData(xmlData, "Name"),
                                     CompanyGuid = Utility.CheckXmlData(xmlData, "CompanyGuid"),
                                     EMail = Utility.CheckXmlData(xmlData, "EMail"),
                                     MasterGuid = Utility.CheckXmlData(xmlData, "MasterGuid"),
                                     LeaderGuid = Utility.CheckXmlData(xmlData, "LeaderGuid"),
                                     ParentGuid = Utility.CheckXmlData(xmlData, "ParentGuid"),
                                     Telephone = Utility.CheckXmlData(xmlData, "Telephone"),
                                     Sort = int.Parse(Utility.CheckXmlData(xmlData, "Order"))
                                 };
                    context.AddToBranch(branch);
                }
                else
                {
                    branch.Name = Utility.CheckXmlData(xmlData, "Name");
                    branch.CompanyGuid = Utility.CheckXmlData(xmlData, "CompanyGuid");
                    branch.EMail = Utility.CheckXmlData(xmlData, "EMail");
                    branch.MasterGuid = Utility.CheckXmlData(xmlData, "MasterGuid");
                    branch.LeaderGuid = Utility.CheckXmlData(xmlData, "LeaderGuid");
                    branch.ParentGuid = Utility.CheckXmlData(xmlData, "ParentGuid");
                    branch.Telephone = Utility.CheckXmlData(xmlData, "Telephone");
                    branch.Sort = int.Parse(Utility.CheckXmlData(xmlData, "Order"));
                }
                var result = context.SaveChanges();
                return result == 1 ? 0 : 1;
            }
            catch
            {
                throw new Exception("写入Branch数据时出错！");
            }
        }
        #endregion
        #region\\transform
        public System.Xml.XmlNode TransformTaskData(OUDataInfo userInfo, System.Xml.XmlNode xmlParam, orgEntities context, BaseTaskDataSource baseInfo)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
